//
//  GAKKIBaseNavigationController.m
//  GANGAIKit
//
//  Created by fh on 2019/11/15.
//  Copyright © 2019 cashchina. All rights reserved.
//

#import "GAKKIBaseNavigationController.h"

@interface GAKKIBaseNavigationController () <UIGestureRecognizerDelegate>

@end

@implementation GAKKIBaseNavigationController

- (void)dealloc {
    if (kDebugViewControllerRelease) {
        DBLog(@"\n ~~~~~~+++++++ 控制器 %@ 销毁了 +++++++~~~~~~ \n",[self class])
    }
}

// 第一次使用这个类调用一次
+ (void)initialize {
    // 设置UINavigationBar的主题
    [self innerSetupNavigationBarTheme];
    
    // 设置UIBarButtonItem的主题
    [self innerSetupBarButtonItemTheme];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // 解决自定义导航栏之后侧滑返回手势失效的bug
    self.interactivePopGestureRecognizer.delegate = self;
}

#pragma mark - Theme
/**
 *  设置UINavigationBarTheme的主题
 */
+ (void)innerSetupNavigationBarTheme {
    UINavigationBar *appearance = [UINavigationBar appearance];
    [appearance setTranslucent:NO];
    // 设置导航栏的样式
    [appearance setBarStyle:UIBarStyleDefault];
    //设置导航栏的渲染色
    [appearance setTintColor:[UIColor whiteColor]];
    // 设置导航栏的背景色
    [appearance setBarTintColor:[UIColor orangeColor]];

    // 设置导航栏的背景图片
//    [appearance setBackgroundImage:[UIImage imageNamed:@"ic_navigationBar"] forBarPosition:UIBarPositionAny barMetrics:UIBarMetricsDefault];
    
//    UIImage *backGroundImage = [UIImage imageNamed:@"ic_navigationBar"];
//    backGroundImage = [backGroundImage resizableImageWithCapInsets:UIEdgeInsetsZero resizingMode:UIImageResizingModeStretch];
//    [appearance setBackgroundImage:backGroundImage forBarPosition:UIBarPositionAny barMetrics:UIBarMetricsDefault];
//
//    [appearance setTitleTextAttributes: [NSDictionary dictionaryWithObjectsAndKeys:[UIColor blueColor], NSForegroundColorAttributeName,[UIFont systemFontOfSize:18], NSFontAttributeName, nil]];
    
    // 设置背景图片
//    [appearance setBackgroundImage:MHImageNamed(@"navigationbarBackgroundWhite") forBarMetrics:UIBarMetricsDefault];
    
    // 设置文字属性
    NSMutableDictionary *textAttrs = [NSMutableDictionary dictionary];
    textAttrs[NSFontAttributeName] = [UIFont systemFontOfSize:19];
    textAttrs[NSForegroundColorAttributeName] = [UIColor whiteColor];
    // UIOffsetZero是结构体, 只要包装成NSValue对象, 才能放进字典\数组中
    NSShadow *shadow = [[NSShadow alloc] init];
    shadow.shadowOffset =  CGSizeZero;
    textAttrs[NSShadowAttributeName] = shadow;
    [appearance setTitleTextAttributes:textAttrs];
    
    appearance.translucent = NO;
    
    // 修改导航栏分割线颜色:废弃
//    [[UINavigationBar appearanceWhenContainedIn:self, nil] setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
//    [[UINavigationBar appearanceWhenContainedIn:self, nil] setShadowImage:[UIImage new]];
    //修改导航栏分割线颜色
    if (@available(iOS 9.0, *)) {
        [[UINavigationBar appearanceWhenContainedInInstancesOfClasses:@[[self class]]] setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
        [[UINavigationBar appearanceWhenContainedInInstancesOfClasses:@[[self class]]] setShadowImage:[UIImage new]];
    }
}
/**
 *  设置UIBarButtonItem的主题
 */
+ (void)innerSetupBarButtonItemTheme {
    // 通过appearance对象能修改整个项目中所有UIBarButtonItem的样式
    UIBarButtonItem *appearance = [UIBarButtonItem appearance];
    // 设置字体大小
    CGFloat fontSize = 16.0f;
    // 设置字体颜色
    UIColor *color = [UIColor whiteColor];
    
    /** 设置文字属性 **/
    // 设置普通状态的文字属性
    NSMutableDictionary *textAttrs = [NSMutableDictionary dictionary];
    textAttrs[NSForegroundColorAttributeName] = color; // blackColor
    textAttrs[NSFontAttributeName] = [UIFont boldSystemFontOfSize:fontSize];
    NSShadow *shadow = [[NSShadow alloc] init];
    shadow.shadowOffset =  CGSizeZero;
    textAttrs[NSShadowAttributeName] = shadow;
    [appearance setTitleTextAttributes:textAttrs forState:UIControlStateNormal];
    
//     设置高亮状态的文字属性
//    NSMutableDictionary *highTextAttrs = [NSMutableDictionary dictionaryWithDictionary:textAttrs];
//    highTextAttrs[NSForegroundColorAttributeName] = [[UIColor blackColor] colorWithAlphaComponent:.5f]; // blackColor
//    [appearance setTitleTextAttributes:highTextAttrs forState:UIControlStateHighlighted];
    
    // 设置不可用状态(disable)的文字属性
//    NSMutableDictionary *disableTextAttrs = [NSMutableDictionary dictionaryWithDictionary:textAttrs];
//    disableTextAttrs[NSForegroundColorAttributeName] = [[UIColor colorFromHexString:@"#000000"]  colorWithAlphaComponent:.5f]; // #FFFFFF
//    [appearance setTitleTextAttributes:disableTextAttrs forState:UIControlStateDisabled];
    
}

#pragma mark - Event
- (void)innerBackItemDidClicked {
    [self popViewControllerAnimated:YES];
}

#pragma mark - Override
/// 能拦截所有push进来的子控制器
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated {
    
    // 如果现在push的不是栈底控制器(最先push进来的那个控制器)
    if (self.viewControllers.count > 0){

        /// 统一隐藏底部tabbar
        viewController.hidesBottomBarWhenPushed = YES;
        
        /// CoderMikeHe Fixed Bug: 隐藏掉系统的返回按钮，这种方法会导致, 侧滑返回取消，导航栏会 莫名显示三个蓝点，及其恶心。但是如果不隐藏返回按钮，则会出现，你在viewController里面设置其 navigationItem.leftBarButtonItem = nil; 就会显示出系统的返回按钮。
        viewController.navigationItem.backBarButtonItem = nil;
        [viewController.navigationItem setHidesBackButton:YES];
        
        // 这里可以设置导航栏的左右按钮 统一管理方法
        viewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(innerBackItemDidClicked)];
        
//        viewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"navigationBar_back"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(innerBackItemDidClicked)];
        
    }
  
    [super pushViewController:viewController animated:animated];
    
    // 修改tabBra的frame
    CGRect frame = self.tabBarController.tabBar.frame;
    frame.origin.y = [UIScreen mainScreen].bounds.size.height - frame.size.height;
    self.tabBarController.tabBar.frame = frame;
}

- (BOOL)shouldAutorotate {
    return self.topViewController.shouldAutorotate;
}

- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
    return self.topViewController.supportedInterfaceOrientations;
}

- (UIStatusBarStyle)preferredStatusBarStyle {
    return self.topViewController.preferredStatusBarStyle;
}
 
- (BOOL)prefersStatusBarHidden {
    return self.topViewController.prefersStatusBarHidden;
}

#pragma mark - UIGestureRecognizerDelegate
// 解决自定义导航栏之后侧滑返回手势失效的bug
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {
    return self.childViewControllers.count > 1;
}

@end
